package com.yc.spring.mvc.core;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yc.spring.mvc.core.annotation.RequestParam;

public class HandleRequest {
	protected static Object[] handle(HttpServletRequest request, Method method, HttpServletResponse response) throws InstantiationException, IllegalAccessException {
		Object[] args = null;
		
		//数组的长度 ->由这这个方法的形参数量决定
		int count = method.getParameterCount();
		args = new Object[count];
		
		//获取这个方法的参数列表
		Parameter[] params = method.getParameters();
		
		String paramName = null;//形参名
		Class<?> cls = null;//形参的类型
		String value = null;
		RequestParam requestParam = null;
		int index = 0;//形参索引下标
		Map<String, String[]> paramValues = null;
		Map<String, Object> paramMap = null;
		Field[] fields = null;
		Object obj = null;
		String attrName = null;
		Class<?> attrType = null;
		
		for(Parameter param : params) {
			paramName = param.getName();
			
			cls = param.getType(); //获取形参的类型
			requestParam = param.getAnnotation(RequestParam.class);
			if(requestParam != null) {//说明以Map方式注值
				paramName = requestParam.value();//获取用户指定的参数名
			}
			
			value = request.getParameter(paramName);//根据形参名从请求中获取参数值
			
			//注值
			if(cls == Integer.TYPE) {
				args[index] = Integer.parseInt(value);
			}else if(cls == Integer.class){
				args[index] = Integer.valueOf(value);
			}else if(cls == Float.TYPE){
				args[index] = Float.parseFloat(value);
			}else if(cls == Float.class){
				args[index] = Float.valueOf(value);
			}else if(cls == Double.TYPE){
				args[index] = Double.parseDouble(value);
			}else if(cls == Double.class){
				args[index] = Double.valueOf(value);
			}else if(cls == String.class){
				args[index] = value;
			}else if(cls == Map.class){
				paramValues = request.getParameterMap();
				paramMap = new HashMap<String, Object>();
				for(Entry<String, String[]> entry : paramValues.entrySet()) {
					paramMap.put(entry.getKey(), entry.getValue()[0]);
				}
				args[index] = paramMap;
			}else if(cls == ServletRequest.class || cls == HttpServletRequest.class) {
				args[index] = request;
			}else if(cls == ServletResponse.class || cls == HttpServletResponse.class) {
				args[index] = response;
			}else if(cls == HttpSession.class) {
				args[index] = request.getSession();
			}else if(cls == ServletContext.class) {
				args[index] = request.getServletContext();
			}else {//当成实体类处理
				fields = cls.getDeclaredFields(); // 获取这个实体类中的所有属性
				obj = cls.newInstance(); // 实例化一个这个类的对象
				
				for(Field fd : fields) {
					fd.setAccessible(true);
					
					attrName = fd.getName();
					attrType = fd.getType();
					
					value = request.getParameter(attrName);
					if(StringUtil.chekNUll(value)) {//如果根据属性名取到的属性值为空，这说明属性不需要注值
						continue;
					}
					
					if(attrType == Integer.TYPE) {
						fd.set(obj, Integer.parseInt(value));
					}else if(attrType == Integer.class){
						fd.set(obj, Integer.valueOf(value));
					}else if(attrType == Float.TYPE){
						fd.set(obj, Float.parseFloat(value));
					}else if(attrType == Float.class){
						fd.set(obj, Float.valueOf(value));
					}else if(attrType == Double.TYPE){
						fd.set(obj, Double.parseDouble(value));
					}else if(attrType == Double.class){
						fd.set(obj, Double.valueOf(value));
					}else {
						fd.set(obj, value);
					}
				}
				args[index] = obj;
			}
			++ index;
		}
		return args;
	}
}
